<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/sys/include/taglib.jsp" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<%@ include file="/WEB-INF/adminui/include/css.jsp" %>
<title>权限列表</title>
</head>
<body class="gray-bg">
<div class="wrapper wrapper-content ">
	<div class="row">
		<div class="col-sm-2">
			<div class="ibox ibox-body">
				<div class="ibox-title">
					<h5>选择权限项</h5>
				</div>
				<div class="ibox-content">
					<div id="jstree"></div>
				</div>
			</div>
		</div>
		<div class="col-sm-9">
			<div class="ibox">
				<div class="ibox-body">
					<div class="fixed-table-toolbar">
						<div id="toolbar" class="columns pull-left">
							<button type="button" class="btn btn-primary" onclick="add()">
								<i class="fa fa-plus hidden" aria-hidden="true"></i>添加
							</button>
							<button type="button" class="btn btn-danger" onclick="del()">
								<i class="fa fa-trash hidden" aria-hidden="true"></i>删除
							</button>
						</div>
						<div class="columns pull-right">
							<button class="btn btn-success" onclick="search()">查询</button>
						</div>
						<div class="columns pull-right col-md-2 nopadding">
							<input id="searchName" type="text" class="form-control" placeholder="姓名">
						</div>
					</div>
					<table id="dataTable" data-mobile-responsive="true">
					</table>
				</div>
			</div>
		</div>
	</div>
</div>
<%@ include file="/WEB-INF/adminui/include/script.jsp" %>
<script type="text/javascript">
var context_path='${path}', leftdata, treeNode, table, layerIndex;

$(function(){
	$('#jstree').data('jstree', false).empty();
	$('#jstree').jstree({
		core:{
			data: {
				url: context_path+'/perm/permJstree.json',
				data: function (node) {
					//console.log(node.original)
					return {pid : node.id};
				}
			},
			multiple: false
		}
	}).on('select_node.jstree', function (event, data){
		treeNode = data.node;
		leftdata = data.node.id;
		$('#jstree').jstree("open_node", data.node);//refresh_node
		loadTable();
    }).on('loaded.jstree', function(e, data){
    	var tree = data.instance,
    		obj = tree.get_node(e.target.firstChild.firstChild.lastChild);
    	tree.select_node(obj);  
    }).on('load_node.jstree', function(e, data){
    	var tree = data.instance, pnode = data.nade;
	});
	
	$('#jstree').parent().css({"maxHeight": ($(window).height()-100)+"px", "overflow":"auto"});
});

function loadTable(){
	if(table){
		$("#dataTable").bootstrapTable('refresh');
	}else{
		createTable();
	}
}
function refreshTree(){
	$('#jstree').jstree("refresh_node", treeNode);
}
function createTable(){
	table = $("#dataTable").bootstrapTable({
		url: context_path + "/perm/listdata.json",
		cache: false,	//禁用 AJAX 数据缓存
		striped: true,	//表格显示条纹
		pagination: false,//显示分页组件
		pageList: [15, 20, 50],
		pageSize: 15,
		pageNumber: 1,
		sidePagination: 'server',
		queryParams: function(params){
			return {
				page: parseInt(params.offset / params.limit) + 1,
				pagesize: params.limit,
				sortname: params.sort || '',
				sortorder: params.order,
				leftdata: leftdata
	          };
		},
		columns: [
			{checkbox : true},
			{field : 'perid', title : 'ID'},
			{field : 'per_code', title : '编号'},
			{field : 'per_name', title : '名称'},
			{field : 'remark', title : '描述'},
			{field : 'url', title : 'URL'},
			{field : 'per_index', title : '顺序号'},
			{
				title : '操作',
				field : 'perid',
				align : 'center',
				formatter : function(value, row, index) {
					var s = '<a class="btn btn-primary btn-sm" href="javascript:;" title="修改" onclick="edit(\'' + value + '\')"><i class="fa fa-edit"></i></a>';
					return s;
				}
			}
		]
	});
}

function add(){
	layerIndex = layer.open({
		type : 2,
		title : '添加权限项',
		maxmin : true,
		shadeClose : false, // 点击遮罩关闭层
		area : [ '800px', '520px' ],
		content : context_path + '/perm/add.do?leftdata='+leftdata
	});
}
function del(){
	var rows = $('#dataTable').bootstrapTable('getSelections');
	if (rows.length == 0) {
		layer.msg("请选择要删除的数据");
		return;
	}
	layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
		btn : [ '确定', '取消' ]
	// 按钮
	}, function() {
		var ids = [];
		$.each(rows, function(i, row) {
			ids.push('perid='+row.perid);
		});
		$.ajax({
			type : 'POST',
			data : ids.join('&'),
			url : context_path + '/perm/delete.json',
			success : function(data) {
				if(!UiUtil.hasErrors(data)){
					layer.msg("删除权限项成功.", {icon: 1});
					loadTable();
					refreshTree();
				}
			}
		});
	}, function() {});
}
function edit(perid){
	layerIndex = layer.open({
		type : 2,
		title : '修改权限项',
		maxmin : true,
		shadeClose : false,
		area : [ '800px', '520px' ],
		content : context_path + '/perm/edit.do?perid='+perid
	});
}
function uiRefresh(succeeded, refresh){
	layer.close(layerIndex);
	if(!succeeded){
		return;
	}
	if(!refresh){
		loadTable();
		refreshTree();
	}else{
		if(refresh=='all' || refresh=='table' || refresh===true)
		{
			loadTable();
		}
		if(refresh=='all' || refresh=='left' || refresh===true)
		{
			refreshTree();
		}
	}
}
</script>
</body>
</html>